//
//  TTLiveDetailedView.swift
//  TTProduct
//
//  Created by Toj on 3/24/21.
//  Copyright © 2021 TTProduct. All rights reserved.
//

import Foundation

class TTLiveDetailedView: TTLiveStyleView {
    
    override var model: NSObject? {
        didSet {
            let stringNumber = arc4random_uniform(100000)
            residentView.nicknameLabel.stringValue = "博伟\(stringNumber)"
            setupRemakeLayoutSubviews()
        }
    }
    
    // 悬浮: 展示View
    // 放大窗口
    lazy var zoomButton: TTButton = {
        let view = TTButton()
        view.tt_addTarget(self, action: #selector(self.didTapZoom(sender:)), for: .leftMouseUp)
        return view
    }()
    
    // 常驻: 展示信息
    lazy var residentView: TTLiveDetailedResidentView = {
        let view = TTLiveDetailedResidentView()
        return view
    }()
    
    // 工具: 交互
    // 举手
    lazy var handButton: TTButton = {
        let view = TTButton()
        view.tt_setImage(NSImage(named: "liveItemhands"), for: .normal)
        return view
    }()
   
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        
        setupMakeAddSubviews()
        setupMakeUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layout() {
        super.layout()
        
        setupMakeLayoutSubviews()
    }
}

extension TTLiveDetailedView {
    private func setupZoom(is selected: Bool) {
        var normal  = "liveDetailedEnlargedNormal"
        var hover   = "liveDetailedEnlargedHover"
        var pressed = "liveDetailedEnlargedPressed"
        
        if selected {
            normal  = "liveDetailedResumeNormal"
            hover   = "liveDetailedResumeHover"
            pressed = "liveDetailedResumePressed"
        }
        
        zoomButton.tt_setImage(NSImage(named: normal), for: .normal)
        zoomButton.tt_setImage(NSImage(named: hover), for: .hover)
        zoomButton.tt_setImage(NSImage(named: pressed), for: .press)
    }
}

extension TTLiveDetailedView {
    
    @objc private func didTapZoom(sender: TTButton) {
        sender.tt_selected = !sender.tt_selected
        
        setupZoom(is: sender.tt_selected)
    }
}

extension TTLiveDetailedView {
    
    private func setupMakeAddSubviews() {
        
        addSubview(residentView)
        addSubview(zoomButton)
        addSubview(handButton)
    }
   
    private func setupRemakeLayoutSubviews() {
        residentView.nicknameLabel.sizeToFit()
        var nicknameWidth = residentView.nicknameLabel.frame.size.width
        if nicknameWidth > 68 {
            nicknameWidth = 68
        }
        residentView.snp.remakeConstraints { (make) in
            make.left.equalTo(4)
            make.bottom.equalTo(snp.bottom).offset(-4)
            make.height.equalTo(23)
            var width: CGFloat = 5 + 18 + 5
            if nicknameWidth != 0
            { width = width + nicknameWidth + 5 }
            
            make.width.equalTo(width)
        }
    }
    
    private func setupMakeLayoutSubviews() {
        
        setupRemakeLayoutSubviews()
        
        zoomButton.snp.remakeConstraints { (make) in
            make.width.equalTo(23)
            make.height.equalTo(23)
            make.top.equalTo(4)
            make.right.equalTo(snp.right).offset(-4)
        }
        
        handButton.snp.remakeConstraints { (make) in
            make.width.equalTo(23)
            make.height.equalTo(23)
            make.left.equalTo(residentView.snp.right).offset(8)
            make.centerY.equalTo(residentView)
        }
    }
    
    private func setupMakeUI() {
        zoomButton.isHidden = true
        setupZoom(is: false)
        // handButton.isHidden = true
    }
}
